#include "cjdevice.h"
#include <QDebug>
CJDevice::CJDevice()
{
    SignalSlotsInit();
    this->moveToThread(&thread);
    thread.start();
    emit Signal_ToCreateMembers();//创建成员
    modbus->ConnectSignalSlots();//成员创建后再连接信号与槽
    SlotsInit();
}

CJDevice::~CJDevice()
{
    thread.quit();
    thread.wait();
}

void CJDevice::OpenPort(QString portName,int baudRate)
{
    modbus->port.setBaudRate(baudRate);
    emit Signal_ToOpenPort(portName);
}

void CJDevice::ClosePort()
{
    emit Signal_ToClosePort();
}

void CJDevice::SignalSlotsInit()
{

    connect(this,SIGNAL(Signal_ToCreateMembers()),this,SLOT(CreateMembers()),Qt::BlockingQueuedConnection);
}

void CJDevice::StopTimer()
{
    emit Signal_ToStopTimer();
}

void CJDevice::StartTimer()
{
    emit Signal_ToStartTimer();
}

void CJDevice::SlotsInit()
{
    qRegisterMetaType<AGroupBattery>("AGroupBattery");
    connect(this,SIGNAL(Signal_ToOpenPort(QString)),this,SLOT(Slots_OpenPort(QString)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToClosePort()),this,SLOT(Slots_ClosePort()),Qt::BlockingQueuedConnection);

    connect(timer,SIGNAL(timeout()),this,SLOT(Slots_TimerTimeOut()));//成员创建后才关联信号与槽
    connect(this,SIGNAL(Signal_ToStartTimer()),this,SLOT(Slots_ToStartTimer()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToStopTimer()),this,SLOT(Slots_ToStopTimer()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_SendFc16(unsigned char, ushort, ushort, ushort*,bool *)),this,SLOT(Slot_SendFc16(unsigned char, ushort, ushort, ushort*,bool*)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Siganl_SendFc3(unsigned char, ushort, ushort,ushort *,bool *)),this,SLOT(Slot_SendFc3(unsigned char, ushort, ushort,ushort *,bool *)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_SendFc6(unsigned char, ushort, ushort, ushort*)),this,SLOT(Slots_sendFc6(unsigned char, ushort, ushort, ushort*)),Qt::BlockingQueuedConnection);

}

void CJDevice::SendFc3(unsigned char a, ushort b, ushort c, ushort *d, bool *e)
{
    emit  Siganl_SendFc3(a, b, c,d,e);
}

void CJDevice::SendFc16(unsigned char a, ushort b, ushort c, ushort *d, bool *e)
{
    emit Signal_SendFc16(a,b,c,d,e);
}

void CJDevice::SendFc6(unsigned char address, ushort start, ushort registers, ushort *values)
{
     emit Signal_SendFc6(address, start, registers, values);
}

void CJDevice::CreateMembers()
{
    modbus=new Modbus();
    modbus->port.setParity(QSerialPort::NoParity);
    modbus->port.setDataBits(QSerialPort::Data8);
    modbus->port.setStopBits(QSerialPort::OneStop);
    modbus->port.setBaudRate(9600);
    timer=new QTimer();
}

void CJDevice::Slots_OpenPort(QString value)
{
    modbus->port.setPortName(value);
    modbus->port.open(QSerialPort::ReadWrite);
    if(modbus->port.isOpen())
    {
        qDebug()<<"open sucess";
        timer->start(2000);
    }
    else
    {
        qDebug()<<"open failed";
    }
}

void CJDevice::Slots_ClosePort()
{
    qDebug()<<__FUNCTION__;
    modbus->port.close();
}

void CJDevice::Slots_TimerTimeOut()
{
    if(batteryNumber>0&&batteryNumber<=255)
    {
        ushort *recv=new ushort[batteryNumber*3];
        bool ret=false;
        ret=modbus->SendFc3(1,50,batteryNumber*3,recv);
        if(ret)
        {
            for(int i=0;i<=batteryNumber-1;i++)
            {
                gropBattery.battery[i].voltage=(float)recv[0+3*i]/1000;
                gropBattery.battery[i].temperature=(float)recv[1+3*i]/10;
                gropBattery.battery[i].NeiZhu=(float)recv[2+3*i]/1000;

            }
            emit Siganl_ToShowValueData(gropBattery);
        }
        delete [] recv;
    }
}

void CJDevice::Slots_ToStartTimer()
{
    qDebug()<<__FUNCTION__<<__LINE__<<"startTimer";
    timer->start(2000);
}

void CJDevice::Slots_ToStopTimer()
{
    timer->stop();
}

void CJDevice::Slot_SendFc16(unsigned char a, ushort b , ushort c , ushort *d, bool *e)
{
    bool ret=false;
    qDebug()<<__FUNCTION__<<__LINE__;
    ret=modbus->SendFc16(a,b,c,d);
    qDebug()<<__FUNCTION__<<__LINE__;
    *e=ret;
}

void CJDevice::Slot_SendFc3(unsigned char a, ushort b , ushort c, ushort *d, bool *e)
{
    bool ret=false;
    ret=modbus->SendFc3(a,b,c,d);
    *e=ret;
}

void CJDevice::Slots_sendFc6(unsigned char address, ushort start, ushort registers, ushort *values)
{
    bool ret=false;
    ret=modbus->SendFc6(address,start,registers,values);
}
